package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-15
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    /**
     * 先删除视频 ，删视频实际就是删除数据表里的source id  再删除小节
     * @param courseId
     */
    @Override
    public void removeVideoByCourseId(String courseId) {
        //1.根据课程id查询出课程里的所有视频id
        QueryWrapper<EduVideo> wrappervideo=new QueryWrapper<>();
        wrappervideo.eq("course_id",courseId);
        //只查询video_source_id
        wrappervideo.select("video_source_id");
        List<EduVideo> eduVideoList = baseMapper.selectList(wrappervideo);
        //list<EduVideo>变成list<String>
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < eduVideoList.size(); i++) {
            EduVideo video = eduVideoList.get(i);
            String videoSourceIds=video.getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceIds)){
                list.add(videoSourceIds);
            }
        }
        if (list.size()>0){
            vodClient.deleteBatch(list);
        }

        //2 根据课程id删除小节
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
